using System;
using System.Data;
using System.Configuration;
using System.Text;
using System.IO;

namespace VSM.Utils
{
    public class EmailGenerator
    {
        #region Fields
        protected string SourceContent = string.Empty;
        protected string ErrMsg = string.Empty;
        private string mail_title = string.Empty;
        #endregion

        #region Properties
        public string MailTitle
        {
            get { return this.mail_title; }
        }
        #endregion

        #region Methods
        public EmailGenerator(string SourceFile, string LanguageID)
        {
            // get the file name
            string FileName = SourceFile + "-" + LanguageID + ".htm";
            // read the file
            using (StreamReader FileReader = new StreamReader(FileName, Encoding.UTF8))
            {
                SourceContent = FileReader.ReadToEnd();
                FileReader.Close();
            }
        }

        #region POSubmmitted
        public string POSubmmitted(string ApproverName, string PONo, string LinkUrl)
        {
            StringBuilder EmailBuilder = new StringBuilder(this.SourceContent);

            if (this.SourceContent.Length > 0)
            {
                // replace with data
                EmailBuilder.Replace("#EmployeeName#", ApproverName);
                EmailBuilder.Replace("#ReferenceNo#", PONo);
                EmailBuilder.Replace("#DateSubmitted#", DateTime.Now.ToString("dd/MM/yyyy"));
                EmailBuilder.Replace("#Link#", LinkUrl);
                // title
                this.mail_title = this.SourceContent.Substring(
                    this.SourceContent.IndexOf("<title>") + "<title>".Length,
                    this.SourceContent.IndexOf("</title>") - (this.SourceContent.IndexOf("<title>") + "<title>".Length));
            }
            else
            {
                this.ErrMsg = "[EmailGenerator.POSubmmitted] : No source file";
            }

            return EmailBuilder.ToString();
        }
        #endregion

        #region POApproved
        public string POApproved(string BuyerName, string PONo, string ApproverName, string LinkUrl)
        {
            StringBuilder EmailBuilder = new StringBuilder(this.SourceContent);

            if (this.SourceContent.Length > 0)
            {
                // replace with data
                EmailBuilder.Replace("#EmployeeName#", BuyerName);
                EmailBuilder.Replace("#ReferenceNo#", PONo);
                EmailBuilder.Replace("#ApproverName#", ApproverName);
                EmailBuilder.Replace("#DateApproved#", DateTime.Now.ToString("dd/MM/yyyy"));
                EmailBuilder.Replace("#Link#", LinkUrl);
                // title
                this.mail_title = this.SourceContent.Substring(
                    this.SourceContent.IndexOf("<title>") + "<title>".Length,
                    this.SourceContent.IndexOf("</title>") - (this.SourceContent.IndexOf("<title>") + "<title>".Length));
            }
            else
            {
                this.ErrMsg = "[EmailGenerator.POApproved] : No source file";
            }

            return EmailBuilder.ToString();
        }
        #endregion

        #region PORejected
        public string PORejected(string BuyerName, string PONo, string RejecterName, string LinkUrl)
        {
            StringBuilder EmailBuilder = new StringBuilder(this.SourceContent);

            if (this.SourceContent.Length > 0)
            {
                // replace with data
                EmailBuilder.Replace("#EmployeeName#", BuyerName);
                EmailBuilder.Replace("#ReferenceNo#", PONo);
                EmailBuilder.Replace("#RejecterName#", RejecterName);
                EmailBuilder.Replace("#DateRejected#", DateTime.Now.ToString("dd/MM/yyyy"));
                EmailBuilder.Replace("#Link#", LinkUrl);
                // title
                this.mail_title = this.SourceContent.Substring(
                    this.SourceContent.IndexOf("<title>") + "<title>".Length,
                    this.SourceContent.IndexOf("</title>") - (this.SourceContent.IndexOf("<title>") + "<title>".Length));
            }
            else
            {
                this.ErrMsg = "[EmailGenerator.PORejected] : No source file";
            }

            return EmailBuilder.ToString();
        }
        #endregion

        #region SOSubmmitted
        public string SOSubmmitted(string SaleManName, string SaleManager, string SONo, string SubmmitedName, string DealerName, string LinkUrl)
        {
            StringBuilder EmailBuilder = new StringBuilder(this.SourceContent);

            if (this.SourceContent.Length > 0)
            {
                // replace with data
                EmailBuilder.Replace("#EmployeeName#", SaleManName);
                EmailBuilder.Replace("#SaleManagerName#", SaleManager);
                EmailBuilder.Replace("#ReferenceNo#", SONo);
                EmailBuilder.Replace("#SubmittedBy#", SubmmitedName);
                EmailBuilder.Replace("#DealerName#", DealerName);
                EmailBuilder.Replace("#DateSubmitted#", DateTime.Now.ToString("dd/MM/yyyy"));
                EmailBuilder.Replace("#Link#", LinkUrl);
                // title
                this.mail_title = this.SourceContent.Substring(
                    this.SourceContent.IndexOf("<title>") + "<title>".Length,
                    this.SourceContent.IndexOf("</title>") - (this.SourceContent.IndexOf("<title>") + "<title>".Length));
            }
            else
            {
                this.ErrMsg = "[EmailGenerator.SOSubmmitted] : No source file";
            }

            return EmailBuilder.ToString();
        }
        #endregion

        #region SOUpdated
        public string SOUpdated(string BuyerManager, string SONo, string UpdatedName, string LinkUrl)
        {
            StringBuilder EmailBuilder = new StringBuilder(this.SourceContent);

            if (this.SourceContent.Length > 0)
            {
                // replace with data
                EmailBuilder.Replace("#EmployeeName#", BuyerManager);
                EmailBuilder.Replace("#ReferenceNo#", SONo);
                EmailBuilder.Replace("#UpdatedBy#", UpdatedName);
                EmailBuilder.Replace("#DateUpdated#", DateTime.Now.ToString("dd/MM/yyyy"));
                EmailBuilder.Replace("#Link#", LinkUrl);
                // title
                this.mail_title = this.SourceContent.Substring(
                    this.SourceContent.IndexOf("<title>") + "<title>".Length,
                    this.SourceContent.IndexOf("</title>") - (this.SourceContent.IndexOf("<title>") + "<title>".Length));
            }
            else
            {
                this.ErrMsg = "[EmailGenerator.SOUpdated] : No source file";
            }

            return EmailBuilder.ToString();
        }
        #endregion

        #region VSMSOConfirmed
        public string VSMSOConfirmed(string BuyManager, string SONo, string ConfirmedName, string LinkUrl)
        {
            StringBuilder EmailBuilder = new StringBuilder(this.SourceContent);

            if (this.SourceContent.Length > 0)
            {
                // replace with data
                EmailBuilder.Replace("#EmployeeName#", BuyManager);
                EmailBuilder.Replace("#ConfirmedBy#", ConfirmedName);
                EmailBuilder.Replace("#ReferenceNo#", SONo);
                EmailBuilder.Replace("#DateConfirmed#", DateTime.Now.ToString("dd/MM/yyyy"));
                EmailBuilder.Replace("#Link#", LinkUrl);
                // title
                this.mail_title = this.SourceContent.Substring(
                    this.SourceContent.IndexOf("<title>") + "<title>".Length,
                    this.SourceContent.IndexOf("</title>") - (this.SourceContent.IndexOf("<title>") + "<title>".Length));
            }
            else
            {
                this.ErrMsg = "[EmailGenerator.VSMSOConfirmed] : No source file";
            }

            return EmailBuilder.ToString();
        }
        #endregion

        #region VSMSORejected
        public string VSMSORejected(string BuyManagerName, string SONo, string RejecterName, string LinkUrl)
        {
            StringBuilder EmailBuilder = new StringBuilder(this.SourceContent);

            if (this.SourceContent.Length > 0)
            {
                // replace with data
                EmailBuilder.Replace("#EmployeeName#", BuyManagerName);
                EmailBuilder.Replace("#ReferenceNo#", SONo);
                EmailBuilder.Replace("#RejecterName#", RejecterName);
                EmailBuilder.Replace("#DateRejected#", DateTime.Now.ToString("dd/MM/yyyy"));
                EmailBuilder.Replace("#Link#", LinkUrl);
                // title
                this.mail_title = this.SourceContent.Substring(
                    this.SourceContent.IndexOf("<title>") + "<title>".Length,
                    this.SourceContent.IndexOf("</title>") - (this.SourceContent.IndexOf("<title>") + "<title>".Length));
            }
            else
            {
                this.ErrMsg = "[EmailGenerator.VSMSORejected] : No source file";
            }

            return EmailBuilder.ToString();
        }
        #endregion

        #region DEALERSOConfirmed
        public string DEALERSOConfirmed(string SaleManager, string SONo, string ConfirmedName, string LinkUrl)
        {
            StringBuilder EmailBuilder = new StringBuilder(this.SourceContent);

            if (this.SourceContent.Length > 0)
            {
                // replace with data
                EmailBuilder.Replace("#EmployeeName#", SaleManager);
                EmailBuilder.Replace("#ConfirmedBy#", ConfirmedName);
                EmailBuilder.Replace("#ReferenceNo#", SONo);
                EmailBuilder.Replace("#DateConfirmed#", DateTime.Now.ToString("dd/MM/yyyy"));
                EmailBuilder.Replace("#Link#", LinkUrl);
                // title
                this.mail_title = this.SourceContent.Substring(
                    this.SourceContent.IndexOf("<title>") + "<title>".Length,
                    this.SourceContent.IndexOf("</title>") - (this.SourceContent.IndexOf("<title>") + "<title>".Length));
            }
            else
            {
                this.ErrMsg = "[EmailGenerator.DEALERSOConfirmed] : No source file";
            }

            return EmailBuilder.ToString();
        }
        #endregion

        #region DEALERSORejected
        public string DEALERSORejected(string SaleManagerName, string SONo, string RejecterName, string LinkUrl)
        {
            StringBuilder EmailBuilder = new StringBuilder(this.SourceContent);

            if (this.SourceContent.Length > 0)
            {
                // replace with data
                EmailBuilder.Replace("#EmployeeName#", SaleManagerName);
                EmailBuilder.Replace("#ReferenceNo#", SONo);
                EmailBuilder.Replace("#RejecterName#", RejecterName);
                EmailBuilder.Replace("#DateRejected#", DateTime.Now.ToString("dd/MM/yyyy"));
                EmailBuilder.Replace("#Link#", LinkUrl);
                // title
                this.mail_title = this.SourceContent.Substring(
                    this.SourceContent.IndexOf("<title>") + "<title>".Length,
                    this.SourceContent.IndexOf("</title>") - (this.SourceContent.IndexOf("<title>") + "<title>".Length));
            }
            else
            {
                this.ErrMsg = "[EmailGenerator.DEALERSORejected]: No source file";
            }

            return EmailBuilder.ToString();
        }
        #endregion
        #endregion

    }
}
